package com.iot.core.utils;

import com.iot.core.annotation.Password;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

public class PasswordValidator implements ConstraintValidator<Password, Object> {

    public static final String REGEX_PASSWORD = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z@#$&_.]{6,20}$";

    @Override
    public void initialize(Password password) {
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        if (o == null) {
            return true;
        }
        return Pattern.matches(REGEX_PASSWORD, o.toString());
    }
}
